package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: SetMealServiceImpl
 * Description: com.sky.service.impl
 */
@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult setMealPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setMealMapper.setMealPage(setmealPageQueryDTO);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());

        return pageResult;
    }

    /**
     * 添加套餐和相应的菜品
     * @param setmealDTO
     */
    @Transactional
    public void addSetMealWithDish(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        setMealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();

        // 获取菜品
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();

        if(dishes != null && dishes.size() > 0) {
            for(SetmealDish dish : dishes) {
                dish.setSetmealId(setmealId);
            }

            // 向套餐菜品表插入一条或者多条数据
            setMealDishMapper.insertBatch(dishes);
        }
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Transactional
    public void setMealStatus(Integer status, Long id) {

        // 起售
        if(status == StatusConstant.ENABLE){
            // 套餐内包含未启售菜品，无法启售
            List<String> dishNames =  setMealDishMapper.getDishNameBysetmealId(id);

            for(String dishName : dishNames) {

                if(dishMapper.getDishStatusByDishName(dishName) == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }

            setMealMapper.setMealStatus(status, id);

        }

        // 停售
        if(status == StatusConstant.DISABLE){
            setMealMapper.setMealStatus(status, id);
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    public void deleteSetMeal(List<Long> ids) {

        // 启售中的套餐不能删除
        for(Long id : ids) {

            if(setMealMapper.getSetMealStatusById(id) == StatusConstant.ENABLE) {

                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        // 删除套餐和对应的菜品
        for(Long id : ids) {
            // 这里可以优化，用动态sql减少循环生成的sql语句

            setMealMapper.deleteById(id);

            setMealDishMapper.deleteBySetMealId(id);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getSetmealById(Long id) {
        // 1. 查询套餐基本信息
        SetmealVO setmealVO = setMealMapper.getSetmealById(id);

        // 2. 查询菜品列表
        List<SetmealDish> dishes = setMealDishMapper.getDishBySetmealId(id);

        // 3. 组装数据
        setmealVO.setSetmealDishes(dishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void updateSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        setMealMapper.update(setmeal);

        // 删除套餐菜品表中对应的菜品数据
        setMealDishMapper.deleteBySetMealId(setmeal.getId());

        // 插入你要修改的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {
            for(SetmealDish dish : setmealDishes) {
                dish.setSetmealId(setmeal.getId());
            }

            setMealDishMapper.insertBatch(setmealDishes);

        }
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> getSetmeal(Long categoryId) {

        List<Setmeal> setmeals = setMealMapper.getSetmealByCategoryId(categoryId);

        List<Setmeal> list = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {


            if(setmeal.getStatus() == StatusConstant.ENABLE){
                list.add(setmeal);
            }
        }


        return list;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    public List<DishItemVO> dishList(Long id) {

        List<DishItemVO> dishItemVOList = setMealDishMapper.dishList(id);

        return dishItemVOList;
    }
}
